package com.cloudy.exception;

import java.util.Iterator;
import java.util.List;

import com.cloudy.ec2.response.error.ErrorResponseItemType;
import com.cloudy.ec2.response.error.ErrorResponseType;

public class EC2AccessException extends AWSAccessException{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private ErrorResponseType errorResponse;
	
	public EC2AccessException() {
		super();
	}
	public EC2AccessException(ErrorResponseType error) {
		super();
		this.setErrorResponse(error);
	}
	public EC2AccessException(String message) {
		super(message);
	}
	
	public EC2AccessException(Throwable throwable) {
		super(throwable);
	}
	public EC2AccessException(String message, Throwable throwable) {
		super(message, throwable);
	}
	
	public void setErrorResponse(ErrorResponseType errorResponse) {
		this.errorResponse = errorResponse;
	}
	public ErrorResponseType getErrorResponse() {
		return errorResponse;
	}

	@Override
	protected String getMessageFormErrorResponse() {
		if(errorResponse==null)
			return null;
		StringBuilder message = new StringBuilder();
		List<ErrorResponseItemType> list = errorResponse.getErrors().getError();
		for (Iterator<ErrorResponseItemType> iterator = list.iterator(); iterator.hasNext();) {
			ErrorResponseItemType errorResponseItemType = iterator.next();
			message.append(errorResponseItemType.getCode()).append(": ").append(errorResponseItemType.getMessage()).append("\n");
			
		}
		return message.toString();
	}

}
